/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package provider.impl.coalescent.data;

import coalescent.data.meta.MetaInf_K69Data;
import coalescent.provider.Providers;

import commons.core.Singletons;
import commons.provider.impl.BaseXMLFileBasedProviderImpl_RW;
import commons.util.FormatUtil;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @author Susanta Tewari
 * @version version
 * @history Created on 11/10/13.
 * @since since
 */
public class MetaInf_K69Data_ProviderImpl_RW_XML
        extends BaseXMLFileBasedProviderImpl_RW<MetaInf_K69Data>
        implements Providers.MetaInf_K69Data_Provider_FileBased_RW {

    @Override
    protected MetaInf_K69Data toValue(Element root) throws Exception {

        Map<Double, SortedMap<Date, BigDecimal>> probMap = new HashMap<>();
        final List<Element> thetaList                    = root.elements("theta");

        for (Element theta : thetaList) {

            double theta_value                      = Double.valueOf(theta.attributeValue("value"));
            final TreeMap<Date, BigDecimal> treeMap = new TreeMap<>();

            probMap.put(theta_value, treeMap);

            final List<Element> history = theta.elements("history");

            for (Element entry : history) {

                Date date       = Singletons.DATE.of(entry.attributeValue("time"));
                BigDecimal prob = new BigDecimal(entry.attributeValue("prob"));

                treeMap.put(date, prob);
            }
        }

        return new MetaInf_K69Data(probMap);
    }

    @Override
    protected Element toElement(MetaInf_K69Data metaInf_k69Data, Document document) {

        final String ns      = "coalescent.data.meta.MetaInf_K69Data";
        final Element root_e = document.addElement("root", ns);

        root_e.addAttribute("xmlns", ns);
        root_e.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root_e.addAttribute("xsi:schemaLocation", ns + " MetaInf_K69Data.xsd");

        final Map<Double, SortedMap<Date, BigDecimal>> probMap = metaInf_k69Data.getProb();


        for (Double theta : probMap.keySet()) {

            final Element theta_e = root_e.addElement("theta", ns);

            theta_e.addAttribute("value", FormatUtil.format(theta));

            final SortedMap<Date, BigDecimal> history = probMap.get(theta);

            for (Date date : history.keySet()) {

                final Element history_e = theta_e.addElement("history", ns);

                history_e.addAttribute("time", Singletons.DATE.toString(date));
                history_e.addAttribute("prob", FormatUtil.format(history.get(date)));
            }
        }

        return root_e;
    }
}
